package com.neegix.development.menu.interfaces.controller;

import com.neegix.cqrs.command.UniversalCommandBus;
import com.neegix.cqrs.query.UniversalQueryBus;
import com.neegix.development.menu.application.service.command.DeleteMenuCommand;
import com.neegix.development.menu.application.service.command.UpdateMenuCommand;
import com.neegix.development.menu.application.service.command.mapper.MenuCommandMapper;
import com.neegix.development.menu.application.service.query.GetMenuDetailQuery;
import com.neegix.development.menu.application.service.query.GetMenuQuery;
import com.neegix.development.menu.application.service.query.mapper.MenuQueryMapper;
import com.neegix.development.menu.interfaces.form.CreateMenuForm;
import com.neegix.development.menu.interfaces.form.QueryMenuForm;
import com.neegix.development.menu.interfaces.form.UpdateMenuForm;
import com.neegix.development.menu.interfaces.vo.MenuVO;
import com.neegix.inferfaces.result.Result;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

 /**
  * This file is auto-generated by nebula-framework-generator.
  * The auto-generation plugin was developed using IntelliJ IDEA Community Edition.
  * Thanks to JetBrains for their outstanding contributions to the developer community.
  * <p>
  * The code generated by this tool is owned by the user of the tool.
  * The tool itself is copyrighted by <a href="https://www.neegix.com">https://www.neegix.com</a>.
  *
  * @author <a href="https://www.neegix.com">https://www.neegix.com</a>
  * @version 1.0.0
  * @since 2024-11-21 16:19:51
  */

@RestController
@RequestMapping("/api/v1/menu")
public class MenuController {

    @Autowired
    private UniversalCommandBus commandBus;

    @Autowired
    private UniversalQueryBus queryBus;

    @PreAuthorize("hasAuthority('development:menu:add')")
    @PostMapping
    public Result<Void> createMenu(@RequestBody @Valid CreateMenuForm menuForm){
        Void result = commandBus.execute(MenuCommandMapper.INSTANCE.covertToCreateCommand(menuForm));
        return Result.success("创建成功", result);
    }

    @PreAuthorize("hasAuthority('development:menu:modify')")
    @PutMapping("/{id}")
    public Result<Void> updateMenu(@PathVariable Long id, @RequestBody @Valid UpdateMenuForm menuForm){
        UpdateMenuCommand command = MenuCommandMapper.INSTANCE.covertToUpdateCommand(menuForm);
        command.setId(id);
        Void result = commandBus.execute(command);
        return Result.success("更新成功",result);
    }

    @PreAuthorize("hasAuthority('development:menu:list')")
    @GetMapping
    public Result<List<MenuVO>> getMenus(@RequestBody(required = false) QueryMenuForm menuForm){
        if(menuForm == null) {
            menuForm = new QueryMenuForm();
        }

        GetMenuQuery getMenuQuery = new GetMenuQuery();
        List<MenuVO> data = queryBus.execute(MenuQueryMapper.INSTANCE.covertToGetQuery(menuForm));
        return Result.success("查询成功",data);
    }

    @PreAuthorize("hasAuthority('development:menu:get')")
    @GetMapping("/{id}")
    public Result<MenuVO> getMenuById(@PathVariable("id") Long id) {
        MenuVO menuVO = queryBus.execute(new GetMenuDetailQuery(id));
        return Result.success("获取成功", menuVO);
    }


    @PreAuthorize("hasAuthority('development:menu:remove')")
    @DeleteMapping
    public Result<Void> removeMenu(@RequestBody List<Long> ids){
        return Result.success("删除成功", commandBus.execute(new DeleteMenuCommand(ids)));
    }
}